<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>垃圾投放记录查询</title>
  <style>
    body {
      font-family: 'Arial', sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 0;
      color: #333;
    }
    .container {
      max-width: 1200px;
      margin: 20px auto;
      padding: 20px;
      background-color: #fff;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      border-radius: 8px;
    }
    h1 {
      text-align: center;
      color: #007BFF;
      margin-bottom: 20px;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 20px;
      border-radius: 8px;
      overflow: hidden;
    }
    th, td {
      padding: 12px;
      text-align: center;
      border: 1px solid #ddd;
    }
    th {
      background-color: #007BFF;
      color: #fff;
    }
    tr:nth-child(even) {
      background-color: #f2f2f2;
    }
    tr:hover {
      background-color: #e0e0e0;
    }
    .query-container {
      text-align: center;
      margin-bottom: 20px;
    }
    .query-container input {
      padding: 8px;
      border: 1px solid #ddd;
      border-radius: 4px;
      width: 200px;
    }
    .query-container button {
      padding: 8px 16px;
      background-color: #007BFF;
      color: #fff;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      margin-left: 10px;
    }
    .query-container button:hover {
      background-color: #0056b3;
    }
  </style>
</head>
<body>
<div class="container">
  <div class="header">
    <h1>垃圾投放记录查询</h1>
  </div>
  <div class="query-container">
    <input type="text" id="residentIdInput" placeholder="请输入注册ID">
    <button onclick="fetchWasteRecords()">查询</button>
  </div>
  <table id="wasteRecordsTable">
    <thead>
    <tr>
      <th>编号</th>
      <th>居民编号</th>
      <th>垃圾类型</th>
      <th>投放时间</th>
      <th>重量 (kg)</th>
      <th>投放地点</th>
      <th>审核状态</th>
    </tr>
    </thead>
    <tbody>
    <!-- 查询结果将在此处动态插入 -->
    </tbody>
  </table>
</div>

<script>
  function fetchWasteRecords() {
    const residentId = document.getElementById('residentIdInput').value;
    if (!residentId) {
      alert('请输入注册ID！');
      return;
    }

    fetch(`http://localhost:8081/waste/selectWasteRecordByResidentId?residentId=${residentId}`)
            .then(response => response.json())
            .then(data => {
              console.log('查询结果:', data);
              const tableBody = document.querySelector('#wasteRecordsTable tbody');
              tableBody.innerHTML = ''; // 清除现有记录
              if (data.length === 0) {
                tableBody.innerHTML = '<tr><td colspan="7">没有找到相关记录</td></tr>';
                return;
              }

              data.forEach(record => {
                const row = document.createElement('tr');
                row.innerHTML = `
                        <td>${record.id}</td>
                        <td>${record.residentId}</td>
                        <td>${record.type}</td>
                        <td>${record.time}</td>
                        <td>${record.weight}</td>
                        <td>${record.location}</td>
                        <td>${record.status}</td>
                    `;
                tableBody.appendChild(row);
              });
            })
            .catch(error => {
              console.error('错误:', error);
              alert('查询数据时出错，请稍后再试！');
            });
  }
</script>
</body>
</html>